#!/usr/bin/env python
# -*- coding: utf-8 -*-
#code:utf-8
#
# Copyright(C) 2011 SupDo.com
# Licensed under the GUN License, Version 3.0 (the "License");
# 
# File:        tags.py
# Author:      KuKei
# Create Date: 2011-04-22
# Description: 自定义标签文件
# Modify Date: 2011-04-22

from django import template
from sd.cms.lib.utility import renderToResponse
from sd.cms.lib import Params
from sd.cms.lib.decorator import cacheTag
from sd.cms.models.paramModel import Catalog
from sd.cms.models.appModel import Article, Image, Product

register = template.Library()

class HeadMenuTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.tagName = args[0]
#        self.idName = "headMenu" if len(args)<=1 else args[1].strip('"')
#        self.className = "" if len(args)<=2 else args[2].strip('"')
        
    def render(self, context):
        self.renderHtml = ''                                                                                                                                                               
        return self.renderHtml
    
class ElementTableTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.tagName = args[0]
#        self.idName = "headMenu" if len(args)<=1 else args[1].strip('"')
#        self.className = "" if len(args)<=2 else args[2].strip('"')
        
    def render(self, context):
        self.renderHtml = ''                                                                                                                                                               
        return self.renderHtml

class ElementDetailTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.tagName = args[0]
#        self.idName = "headMenu" if len(args)<=1 else args[1].strip('"')
#        self.className = "" if len(args)<=2 else args[2].strip('"')
        
    def render(self, context):
        self.renderHtml = ''                                                                                                                                                               
        return self.renderHtml
    
#===============================================================================
# 参数说明
# 0：标签名；1：目录名；2：显示个数；3：文章名长度；4：class名；5：标题名字；6：主题名；
#===============================================================================
class ArticleListTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.args = args
        self.tagName = args[0]
        self.catalog = args[1]
        self.cnt = args[2]
        self.length =  args[3]
        self.className = args[4]
        self.titleName = args[5]
        
    @cacheTag(key=Params.ARTICLELIST)
    def render(self, context):
        siteinfo = context.dicts[0]['site'][Params.GLOBALPARAM][Params.GPArticle] if context.dicts[0]['site'][Params.GLOBALPARAM].has_key(Params.GPArticle) else {}
        cataloglist = list(Catalog.objects.filter(value=siteinfo[self.catalog] if siteinfo.has_key(self.catalog) else None))
        catalog = cataloglist[0] if len(cataloglist)==1 else None
        articleList = list(catalog.article_set.filter(status__in=[1,2]).order_by('-crtdate')[0:self.cnt]) if catalog else []
        viewData = {'className':self.className,'titleName':self.titleName,'kindName':'article', 'lists':articleList, 'len':self.length}
        self.renderHtml = renderToResponse('/snippet/listtb.html', viewData, context.dicts[0]['site']['theme'])
        return self.renderHtml._container[0]

#===============================================================================
# 参数说明
# 0：标签名；1：目录名；2：显示个数；3：文章名长度；4：class名；5：标题名字；6：主题名；
#===============================================================================
class ProductListTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.args = args
        self.tagName = args[0]
        self.catalog = args[1]
        self.cnt = args[2]
        self.length =  args[3]
        self.className = args[4]
        self.titleName = args[5]
        
    @cacheTag(key=Params.PRODUCTLIST)
    def render(self, context):
        siteinfo = context.dicts[0]['site'][Params.GLOBALPARAM][Params.GPProduct] if context.dicts[0]['site'][Params.GLOBALPARAM].has_key(Params.GPProduct) else {}
        cataloglist = list(Catalog.objects.filter(value=siteinfo[self.catalog] if siteinfo.has_key(self.catalog) else None))
        catalog = cataloglist[0] if len(cataloglist)==1 else None
        productList = list(catalog.product_set.filter(status__in=[1,2]).order_by('-crtdate')[0:self.cnt]) if catalog else []
        viewData = {'className':self.className,'titleName':self.titleName,'kindName':'product', 'lists':productList, 'len':self.length}
        self.renderHtml = renderToResponse('/snippet/listtb.html', viewData, context.dicts[0]['site']['theme'])
        return self.renderHtml._container[0]
    
class ProductShowTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.args = args
        self.tagName = args[0]
        self.className = args[1]
        
    @cacheTag(key=Params.PRODUCTLIST)
    def render(self, context):
        productList = list(Product.objects.filter(status__in=[1,2],isShow=True).order_by('-crtdate'))
        viewData = {'className':self.className, 'lists':productList}
        self.renderHtml = renderToResponse('/snippet/listshow.html', viewData, context.dicts[0]['site']['theme'])
        return self.renderHtml._container[0]
    
class PicSwitchTag(template.Node):
    renderHtml = ''
    def __init__(self, args):
        self.args = args
        self.tagName = args[0]
        self.kind = args[1]
        self.cnt = args[2] if len(args)>=3 else 8
        self.theme = args[3] if len(args)>=4 else 'default'
        
    @cacheTag(key=Params.IMAGELIST)
    def render(self, context):
        imgList = list(Image.objects.filter(kind=self.kind, isdisplay=1).order_by('orderby'))
        viewData = {'lists':imgList}
        self.renderHtml = renderToResponse('/snippet/PicSwitch.html', viewData, context.dicts[0]['site']['theme'])
        return self.renderHtml._container[0]

def main(parser, token):
    args = token.split_contents()
    #下面字符串中的args就是上一行中的args，eval会将字符串解析成语句
    return eval(args[0]+'Tag(args)')

#用装饰器的方式也行，这样是为了有通用性
register.tag('HeadMenu', main)
register.tag('ElementTable', main)
register.tag('ElementDetail', main)
register.tag('ArticleList', main)
register.tag('ProductList', main)
register.tag('ProductShow', main)
register.tag('PicSwitch', main)
